package com.exchange.controller.app.optional;

import java.util.Date;
import java.util.List;
import com.exchange.common.model.User;
import com.exchange.common.model.UserFavCoin;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import cn.hutool.core.bean.BeanUtil;

public class OptioalService {
	
	
	public Ret optional(User user, String coin, String market) {
		UserFavCoin favCoin = UserFavCoin.dao.findFirst("SELECT id FROM t_user_fav_coin WHERE userId = ? AND coin = ? AND market = ? ", user.getId(), coin, market);
        if(BeanUtil.isEmpty(favCoin)) {
        	new UserFavCoin().setCoin(coin).setMarket(market)
            .setUserId(user.getId()).setCreateTime(new Date()).save();
        }else {
        	UserFavCoin.dao.deleteById(favCoin.getId());
        }
        return Ret.ok();
    }
	
	public Ret favor(User user) {
		List<UserFavCoin> favCoins = UserFavCoin.dao.find("SELECT * FROM t_user_fav_coin WHERE userId = ?",user.getId());		
        return Ret.ok("favor",favCoins);
    }
	
	public Ret favorByCoinTeam(User user, String coin, String market) {
		int count = Db.queryInt("SELECT COUNT(*) FROM t_user_fav_coin WHERE userId = ? AND coin = ? AND market = ? ", user.getId(), coin, market);
		return Ret.ok("isFavor",count>0);
    }
	
}
